var b
var page = 1
// 获取文章id
function getUrlParam(id) {
    var reg = new RegExp("(^|&)" + id + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}
let a = getUrlParam("id"); //id是地址栏的参数



$.ajax({
    type: "GET",
    url: "/getboke/getfindbk",
    data: {
        id: a
    },
    success: (data) => {
        // const obj = data.data.rows
        $(".articlely").append(`
        ${data.data.article}             
          `)
        $.ajax({
            type: "GET",
            url: "/getboke/getfindboke",
            data: {
                id: a,
            },
            success: (data) => {
                $.ajax({
                    type: "GET",
                    url: "/getboke/updateboke",
                    data: {
                        id: a,
                        yuedu:Number(data.data.Reading)+1
                    },
                })
            }
        })
    },
})

$.ajax({
    type: "GET",
    url: "/getpl",
    data: {
        bokeid: a
    },
    success: (data) => {
        const obj = data.data.rows
        for (let i = 0; i < obj.length; i++) {
            $(".mainbar").append(`
            <img class="lytouxiangimg" src="${obj[i].img}">   <i>${obj[i].name}</i>
            <div class="article">
            <h2>${obj[i].text}</h2>
            <p>${obj[i].data}</p>
          </div>
          `)
        }
    },
})

$.ajax({
    type: "GET",
    url: "/getboke/getReading",
    success: (data) => {
        const obj = data.data
        for (let i = 0; i < obj.length; i++) {
            $(".ex_menu").append(`
            <li class="cebianli"><a href="/html/xq.html?id=${obj[i].id}">${obj[i].Boketit}</br>${obj[i].Reading}阅读量</a><br />
            </li>
          `)
        }
    }
})
$("#registersub").click(() => {
    const id = $("#registerid").val()
    const pwd = $("#registerpwd").val()
    const name = $("#registername").val()
    const img = $("#tximg")[0].files
    //构建formdata格式的参数
    const formData = new FormData()
    for (const file of img) {
        formData.append("img", file, file.name)
    }
    $.ajax({
        type: "POST",
        url: "/api/upload",
        data: formData,
        contentType: false,
        // 告诉jQuery不要去设置Content-Type请求头
        processData: false,
        // 告诉jQuery不要去处理发送的数据
        success: (data) => {
            const a = data.data
            $.ajax({
                type: "POST",
                url: "/api/admin/register",
                data: {
                    loginId: id,
                    loginPwd: pwd,
                    name: name,
                    img: `http://localhost:3306${a}`,
                },
                success: (data) => {
                    if (data.data) {
                        $(".container-agille").css("display", "none")
                        $("#registerid").val("")
                        $("#registerpwd").val("")
                        $("#registername").val("")
                        // $(".loginBox").css("display","block")
                    }
                }
            })

        }
    })

})

$(".fenlei_menu").click((e) => {
    if (e.target.tagName == "A") {
        fenlei = $(e.target).text()
        $.ajax({
            type: "POST",
            url: "/getboke/getBookbyfenlei",
            data: {
                fenlei
            },
            success: (data) => {
                if (data.data.rows.length != 0) {
                    b = Math.ceil((data.data.count) / 4)
                    const obj = data.data.rows
                    $(".mainbar").html("")
                    $(".aaa").remove()
                    for (let i = 0; i < obj.length; i++) {
                        $(".mainbar").append(`
                    <div class="articleindex">
                    <h2><span><a href="/html/xq.html?id=${obj[i].id}">${obj[i].Boketit}</a></span></h2>
                    <a href="/html/xq.html?id=${obj[i].id}"<p>${obj[i].Bokecontent}...</p></a>
                    <p>${obj[i].ReleaseTime}       阅读量：${obj[i].Reading}</p>
                  </div>
                  `)
                    }
                    $(".mainbar").append(
                        `
                    <div class="fenye111">
                    <li><img src="../images/left.png" class="fenyeleft"></li>
                    <input tpye="text" class="fenyesousuo">
                    <button  class="buttonfenye">go</button>
                    </div> 
                    `)
                    for (let i = 0; i < b; i++) {
                        $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
                    }
                    $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
                } else {
                    $(".mainbar").html("")
                    $(".mainbar").append(`
                <div class="articleboke">
                没有找到博客
                </div>
                    <img class="cantfind" src="../images/cantfind.jpg">
                  `)
                }
            },
        })
    }
})


$("#loginsub").click(() => {
    let yhname = $("#Username").val() //获取input框用户昵称
    let yhpwd = $("#Password").val() //获取用户密码
    $.ajax({
        type: "POST",
        url: "/api/admin/login",
        data: {
            loginId: yhname,
            loginPwd: yhpwd
        },
        success: (data) => {
            localStorage.setItem("name", data.data.name)
            localStorage.setItem("img", data.data.img)
            if (data.data === null) {
                $(".f_link").css("display", "block")
            } else if (data.data) {
                $(".container-agille").css("display", "none")
                //退出登录
            }
        }

    })
})

//设置cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/"; //path=/是根路径
}


//获取cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";

}

// 清楚cookie方法
function clearCookie() {
    var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    if (keys) {
        for (var i = keys.length; i--;) {
            document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();
            document.cookie = keys[i] + '=0;path=/;domain=localhost' + document.domain + ';expires=' + new Date(0).toUTCString();
            document.cookie = keys[i] + '=0;path=/;domain=localhost;expires=' + new Date(0).toUTCString();
        }
    }
}

$("#pl-submit-btn-main").click(() => {
    if (document.cookie.includes("token")) {
        var $today = new Date();
        var y = $today.getFullYear(); //获取年份
        var m = ($today.getMonth() + 1) < 10 ? ('0' + ($today.getMonth() + 1)) : ($today.getMonth() + 1); //获取月份
        var d = $today.getDate() < 10 ? ('0' + $today.getDate()) : $today.getDate(); //获取天
        var s = $today.toLocaleTimeString();

        $today = y + "-" + m + "-" + d + "-" + s; //获取当前时间
        let text = $("#pl-520am-f-saytext").val() //获取input框用户昵称
        let name = localStorage.name //获取用户密码
        let img = localStorage.img //获取用户密码
        let bokeId = a
        $.ajax({
            type: "POST",
            url: "/addpl/add",
            data: {
                text,
                name,
                img,
                $today,
                bokeId
            },
            success: (data) => {
                $("#pl-520am-f-saytext").val("")
                location.href = location.href
            }
        })
    } else {
        $(".container-agille").css("display", "block")
    }
})

$(".XXX").click(() => {
    $(".container-agille").css("display", "none")
})


$(".mainbar").click((e) => {
    if (e.target.tagName == "LI") {
        page = $(e.target).text()
        $.ajax({
            type: "GET",
            url: "/getboke",
            data: {
                page,
                limit: 4
            },
            success: (data) => {
                $(".mainbar").html("")
                const obj = data.data.rows
                for (let i = 0; i < obj.length; i++) {
                    $(".mainbar").append(`
                <div class="articleindex">
                <h2><span><a href="/html/xq.html?id=${obj[i].id}">${obj[i].Boketit}</a></span></h2>
                <a href="/html/xq.html?id=${obj[i].id}"<p>${obj[i].Bokecontent}...</p></a>
                <p>${obj[i].ReleaseTime}       阅读量：${obj[i].Reading}</p>
              </div>
              `)
                }
                $(".mainbar").append(
                    `
                <div class="fenye111">
                <li><img src="../images/left.png" class="fenyeleft"></li>
                <input tpye="text" class="fenyesousuo">
                <button  class="buttonfenye">go</button>
                </div> 
                `)
                for (let i = 0; i < b; i++) {
                    $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
                }
                $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
            }
        })
    } else if (e.target.tagName == "BUTTON") {
        page = $(".fenyesousuo").val()
        if (page > b) {
            page = b
        }
        $.ajax({
            type: "GET",
            url: "/getboke",
            data: {
                page,
                limit: 4
            },
            success: (data) => {
                $(".mainbar").html("")
                const obj = data.data.rows
                for (let i = 0; i < obj.length; i++) {
                    $(".mainbar").append(`
                    <div class="articleindex">
                    <h2><span><a href="/html/xq.html?id=${obj[i].id}">${obj[i].Boketit}</a></span></h2>
                    <a href="/html/xq.html?id=${obj[i].id}"<p>${obj[i].Bokecontent}...</p></a>
                    <p>${obj[i].ReleaseTime}       阅读量：${obj[i].Reading}</p>
                  </div>
                  `)
                }
                $(".mainbar").append(
                    `
                <div class="fenye111">
                <li><img src="../images/left.png" class="fenyeleft"></li>
                <input tpye="text" class="fenyesousuo">
                <button  class="buttonfenye">go</button>
                </div> 
                `)
                for (let i = 0; i < b; i++) {
                    $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
                }
                $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
            }
        })
    } else if ($(e.target).attr("class") == "fenyeleft") {
        if (page > 1) {
            page = page - 1
            $.ajax({
                type: "GET",
                url: "/getboke",
                data: {
                    page,
                    limit: 4
                },
                success: (data) => {
                    $(".mainbar").html("")
                    const obj = data.data.rows
                    for (let i = 0; i < obj.length; i++) {
                        $(".mainbar").append(`
                    <div class="articleindex">
                    <h2><span><a href="/html/xq.html?id=${obj[i].id}">${obj[i].Boketit}</a></span></h2>
                    <a href="/html/xq.html?id=${obj[i].id}"<p>${obj[i].Bokecontent}...</p></a>
                    <p>${obj[i].ReleaseTime}       阅读量：${obj[i].Reading}</p>
                  </div>
                  `)
                    }
                    $(".mainbar").append(
                        `
                <div class="fenye111">
                 <li><img src="../images/left.png" class="fenyeleft"></li>
                <input tpye="text" class="fenyesousuo">
                <button  class="buttonfenye">go</button>
                </div> 
                `)
                    for (let i = 0; i < b; i++) {
                        $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
                    }
                    $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
                }
            })
        }
    } else if ($(e.target).attr("class") == "fenyeright") {
        if (page < b) {
            page = page + 1
            $.ajax({
                type: "GET",
                url: "/getboke",
                data: {
                    page,
                    limit: 4
                },
                success: (data) => {
                    $(".mainbar").html("")
                    const obj = data.data.rows
                    for (let i = 0; i < obj.length; i++) {
                        $(".mainbar").append(`
                    <div class="articleindex">
                    <h2><span><a href="/html/xq.html?id=${obj[i].id}">${obj[i].Boketit}</a></span></h2>
                    <a href="/html/xq.html?id=${obj[i].id}"<p>${obj[i].Bokecontent}...</p></a>
                    <p>${obj[i].ReleaseTime}       阅读量：${obj[i].Reading}</p>
                  </div>
                  `)
                    }
                    $(".mainbar").append(
                        `
                <div class="fenye111">
                 <li><img src="../images/left.png" class="fenyeleft"></li>
                <input tpye="text" class="fenyesousuo">
                <button  class="buttonfenye">go</button>
                </div> 
                `)
                    for (let i = 0; i < b; i++) {
                        $(".fenye111").append(`<li class="fenyeli${i+1}">${i+1}</li>`)
                    }
                    $(".fenye111").append(` <li><img src="../images/right.png" class="fenyeright"></li>`)
                }
            })
        }
    }
})